from django.db import models

PRES = (
	('A', 'Apress'),
	('B', "O'Reilly"),
	('C', 'Others'),
)

class Author(models.Model):
	'''Author information'''
	name = models.CharField(max_length=100)
	
	def __unicode__(self):
		return self.name

class Tag(models.Model):
	'''Tags collection'''
	name = models.CharField(max_length=300)
	
	def __unicode__(self):
		return self.name

class AbstractBook(models.Model):
	'''Book information'''
	title = models.CharField(max_length=300)
	authors = models.ManyToManyField(Author, through='Tagging')
	press = models.CharField(max_length=1, choices=PRES)
	intro = models.TextField()
	pages = models.IntegerField()
	pub_date = models.DateField()
	isbn = models.CharField(max_length=20)
	tags = models.ManyToManyField(Tag, through='Tagging')
	
	def __unicode__(self):
		return self.title

	class Meta:
		abstract = True

class Book(AbstractBook):
	'''Book status and manager'''
	status = models.CommaSeparatedIntegerField(max_length=20, editable=False)
	# status: <available>, <total>, <reserving>
	num_copy = models.IntegerField()

	def is_available(self):
		avai = int(self.status.split(',')[0])
		return bool(avai)

class Tagging(models.Model):
	'''intermediary for many-to-many relations''' 
	tag = models.ForeignKey(Tag)
	book = models.ForeignKey(Book)
	author = models.ForeignKey(Author)
